<?php
/**
 * 品牌加盟充值管理
 *
 */
defined ( 'InShopNC' ) or exit ( 'Access Invalid!' );
class allianceControl extends BaseMemberControl {
	public function __construct() {
		parent::__construct ();
	}
	
	public function indexOp() {
		// 验证支付方式
		$payment_id =  (int)$_REQUEST ['payment_id'];
		$alliance_deposit = floatval($_REQUEST['alliance_deposit']);
		$brand_id = (int)$_REQUEST['brand_id'];
		if ($payment_id <= 0) {
			appJump ( '请选择支付方式');
		}
		if($alliance_deposit <= 0){
			appJump ( '支付金额错误');
		}
		if($brand_id <= 0){
			appJump ( '参数错误');
		}
		
		$payment_model = new gold_paymentModel ();
		$condition = array ();
		$condition ['payment_id'] = $payment_id;
		$payment_info = $payment_model->getRow ( $payment_id );
		if (empty ( $payment_info ) || $payment_info ['payment_state'] != 1) {
			appJump ( '支付方式不存在或者已关闭，请联系网站管理员');
		}
		 // 预存款实例
		 $insert_arr = array ();
		 $insert_arr ['pdr_sn'] = $pdr_sn = predepositModel::recharge_snOrder ();
		 $insert_arr ['pdr_memberid'] = $_SESSION ['member_id'];
		 $insert_arr ['pdr_membername'] = $_SESSION ['member_name'];
		 $insert_arr ['pdr_price'] = $alliance_deposit;
		 $insert_arr ['pdr_payment'] = $payment_info['payment_code'];
		 $insert_arr ['pdr_remittancename'] = '';
		 $insert_arr ['pdr_remittancebank'] = '';
		 $insert_arr ['pdr_remittancedate'] = '';
		 $insert_arr ['pdr_memberremark'] = '';
		 $insert_arr ['pdr_addtime'] = TIMESTAMP;
		 $insert_arr ['pdr_paystate'] = 0;
		 $insert_arr ['pdr_use'] = PDR_USE_ALLIANCE;
		 $state = Db::insert ( 'predeposit_recharge', $insert_arr );
		 
		 if ($state) {
			$pra_row = array ();
			$pra_row ['pdr_id']  = $state;
			$pra_row ['pdr_sn'] = $pdr_sn;
			$pra_row ['pdr_member_id'] = $_SESSION ['member_id'];
			$pra_row ['pdr_member_name'] = $_SESSION ['member_name'];
			$pra_row ['pdr_brand_id'] = $brand_id;
			$pra_row ['pdr_alliance_amount'] = $alliance_deposit;
			$pra_row ['pdr_pay_state'] = PDR_PAY_STATE_UNPAID;
			Db::insert ( 'predeposit_recharge_alliance', $pra_row );
			
			 // 使用在线支付时跳转到对应的网站
			 $payment_orderinfo = array ();
			 $payment_orderinfo ['order_sn'] = $pdr_sn;
			 $payment_orderinfo ['order_desc'] = '品牌加盟充值';
			 $payment_orderinfo ['order_amount'] = $alliance_deposit;
			 $payment_orderinfo ['discount'] = 0;
			 $payment_orderinfo ['modeltype'] = '3'; // 表示是预存款功能调用支付接口
			 /**
				* 建立接口模型的实例对象
				* 获得支付的url或者表单 数据
				*/
			 $payment_info ['payment_config'] = unserialize ( $payment_info ['payment_config'] );
			 $inc_file = BasePath . DS . 'api' . DS . 'gold_payment' . DS . $payment_info ['payment_code'] . DS . $payment_info ['payment_code'] . '.php';
			 require_once ($inc_file);
			 $payment_api = new $payment_info ['payment_code'] ( $payment_info, $payment_orderinfo );
			 $payment_api->pay_submit ();
		 } else {
			 appJump ( "冲值失败");
		 }
	}

}
